教學來源:
照著步驟完成:Getting Started with CameraX
Google Jetpack 新组件 CameraX 介绍与实践
整理:
camerax最少要Android 5.0 (API level 21)。所以這邊至少要21:
camerax主要三個功能:
Preview(圖像預覽): 看到的畫面設定(還沒要拍照存檔前的畫面)
Image analysis(圖像分析): 取得圖片的數據
Image capture(拍照): 拍下的照片設定,像是閃光
模擬器只有1G跑照相功能會很慢,所以改成2G:
Tool-- >AVD Manager-- >筆-- > show advance setting
想來設定閃光模式,閃光是Image capture(拍照功能)。所以是imageCaptureConfig的設定:
https://developer.android.com/reference/androidx/camera/core/ImageCaptureConfig.Builder
點選FlashMode:
可以看到有三個參數:AUTO(自動閃光)、OFF、ON
所以程式:
val imageCaptureConfig = ImageCaptureConfig.Builder()
.apply {
setFlashMode(FlashMode.AUTO) //閃光自動
}.build()
不過用模擬器,目前不知道有沒有閃光,之後要再測試。(真正的手機有閃光)
想用個按鈕,開關閃光:
參考:Camera flash is not working in android
目前想法:
程式(失敗):
private var flashmode: Boolean = false
btnFlash.setOnClickListener{
if( flashmode == false){
flashmode= true
ImageCaptureConfig.Builder()
.apply {
setFlashMode(FlashMode.ON)
}
btnFlash.setText("閃光啟動中")
}else{
flashmode= false
ImageCaptureConfig.Builder()
.apply {
setFlashMode(FlashMode.OFF)
}
btnFlash.setText("閃光沒開")
}
}
目前想到是直接給個boolean值,如果true就FlashMode.ON,如果false就FlashMode.OFF,不過這樣就要在刷新一次相機。所以要先關閉camerax,在啟動時調整閃光參數。
關於關閉camerax:
What is the correct way of starting and stopping camera using CameraX?
使用:
CameraX.unbindAll()
整理解答:
一 為麼要viewFinder.post { startCamera() }?而不直接startCamera()?
在回去複習https://codelabs.developers.google.com/codelabs/camerax-getting-started/#4
Instead of calling
startCamera()
on the main thread, we useviewFinder.post { ... }
to make sure thatviewFinder
has already been inflated into the view whenstartCamera()
is called.
找到一個camerax的教學:
Introdução a CameraX API
平常設計畫面都只有直的,橫的就會變的很奇怪,實際要調整也很簡單
參考:
How to Create Separate Layout Files for Landscape Mode and Different Screen Sizes - Android Tutorial
Imagebutton點選換圖案,像是閃光有自動、開、關,就可以用三個圖
參考:
Android Imagebutton change Image OnClick
顯示圖案方式:
btn.setImageResource(R.drawable.flash);
想要用兩隻手指放大縮小相機畫面,目前失敗:
目前測試放大縮小只能各一個,而不是慢慢放大,或慢慢縮小
參考:How to zoom the preview using Android CameraX library?
參考解答的程式:https://github.com/Akshayrraiyani/CameraX_Zoom_In_Out
先來了解OnGestureListener、OnTouchListener,教學:
Android Gestures: Getting Started
Android Gestures: Getting Started
GestureDetector中onFling()与onScroll()的区别
Android: Detecting a Pinch Gesture
ViewConfiguration
onTouchListener warning: onTouch should call View#performClick when a click is detected
Listeners with several functions in Kotlin. How to make them shine?
jerrellmardis/ZoomFragment.java
RedApparat/Fotoapparat
Can't change picture resolution
畫素與解析度是什麼?
How to change pictureResolution in Fotoapparat Camera?
Android Camera Library Comparison
就算有firstAvailable()這個,還是會錯誤:
pictureResolution = firstAvailable(
{ Resolution(720, 1280) },
highestResolution()
)
錯誤的時候會顯示,有哪些Resolution:
Resolution configuration selector selected value Resolution(width=720, height=1280). However it's not in the supported set of values. Supported parameters: [Resolution(width=4128, height=3096), Resolution(width=4128, height=2322), Resolution(width=3264, height=2448), Resolution(width=3264, height=1836), Resolution(width=2560, height=1920), Resolution(width=2048, height=1536), Resolution(width=2048, height=1152), Resolution(width=1152, height=1152), Resolution(width=1920, height=1080), Resolution(width=1280, height=960), Resolution(width=1280, height=720), Resolution(width=640, height=480), Resolution(width=320, height=240)]
或是這個錯誤:
io.fotoapparat.exception.camera.InvalidConfigurationException: Resolution configuration selector selected value Resolution(width=1280, height=720). However it's not in the supported set of values. Supported parameters: [Resolution(width=640, height=480), Resolution(width=352, height=288), Resolution(width=320, height=240)]
D/Fotoapparat: Orientations:
Screen orientation (preview) is: Orientation.Vertical.Portrait.
Camera sensor orientation is always at: Orientation.Horizontal.Landscape.
所以如果Portrait(直)的拍照,要自己選轉90度,參考:
Controlling the camera to take pictures in portrait doesn't rotate the final images
how to save bitmap to android gallery
Fotoapparat: Why does landscape CameraView captures in portrait mode?
How to select middle resolution? #377
不過update還是失敗:
fotoapparat?.updateConfiguration(
UpdateConfiguration(
pictureResolution = {
nearestBy(
Resolution(1920, 1080),
Resolution::area
)
}
)
)
不太知道為什麼。是因為修改的是pictureResolution嗎?閃光update就沒問題 。
focusmode 有這些:
[FocusMode.Auto, FocusMode.Infinity, FocusMode.Fixed, FocusMode.Macro, FocusMode.ContinuousFocusVideo, FocusMode.ContinuousFocusPicture]
CameraKit Documentation - v1.0.0-beta3.11
Build出含有版本號的SDK
其他:
這樣就可以看 語法的資訊了:
How to view method information in Android Studio?
選取變數 , 然後 crtl +Q